package org.jianfa.learn.transfer.utils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class TransactionUtil {

    public static Map<Class, Boolean> transactionClass = new HashMap<>();// 判断该类是否开启事务
    public static Map<String, Boolean> transactionMethod = new HashMap<>(); //判断该方法是否需要开启事务

    public static Boolean isTransactionMethod(Method method) {
        Class declaringClass = method.getDeclaringClass();
        boolean isTransactionClass = Boolean.FALSE;
        for (Map.Entry<Class, Boolean> transactionClassEntry : transactionClass.entrySet()) {
            Class transactionClass = transactionClassEntry.getKey();
            if (declaringClass.isAssignableFrom(transactionClass)) {
                isTransactionClass = transactionClassEntry.getValue();
            }
        }

        if (isTransactionClass) {
            String methodIdentifier = MethodUtil.getMethodIdentifier(method);
            if (transactionMethod.containsKey(methodIdentifier)) {
                isTransactionClass = transactionMethod.get(methodIdentifier);
            }
        }

        return isTransactionClass;
    }
}
